/**
 * 却片POI图层
 * @author cqb
 * @requires extend/Layer/PoiLayer.js
 */

OpenLayers.Layer.TiledPoiLayer = OpenLayers.Class(OpenLayers.Layer.PoiLayer, {
	/**
	 * 数据获取地址
	 * @type {String}
	 */
	url: null,
	/**
	 * 根据四至获取poi数据
	 */
	filterByBounds: true,
	
	gridPOIs: null,
	
	iconhalfwidth: 8,
	
	alphaicon: null,
	
	movetimer: null,
	
	initialize: function(name, options){
		OpenLayers.Layer.PoiLayer.prototype.initialize.apply(this, arguments);
		
		this.data = {
			"导向型POI": {
				icon: this.alphaicon,
				align: "center_center",
				data: [{
					id: undefined,
					x: 0,
					y: 0,
					title: "",
					activeIcon: ""
				}]
			}
		};
	},
	
	registerUpdateEvent: function(){
		this.map.events.register("moveend", this, this.redrawByBounds);
		this.map.events.register("mousemove", this, this.updateMarker);
		
		this.createPois(this.map.getExtent());
	},
	
	updateMarker: function(){
		if(this.movetimer){
			window.clearTimeout(this.movetimer);
		}
		var args = arguments,scope = this;
		this.movetimer = window.setTimeout(function(){
			var poi = scope.isMouseOnPOI.apply(scope, args);
			if(poi){
				var marker = scope.markers[0];
				marker.icon.display(true);
				marker.lonlat = new OpenLayers.LonLat(poi.x, poi.y);
				marker.data = poi;
				scope.drawMarker(marker);
				marker.setUrl(poi.POITYPECODE+".png");
				marker.events.triggerEvent("mouseover",{owner: scope, marker: marker});
			}
		}, 100);
	},
	
	/**
	 * 
	 */
	createPois: function(bounds){
		this.initCacheData();
		this._getPOIData(bounds);
		this.createMarkers();
	},
	
	/**
	 * 根据四至等参数获取POI数据
	 */
	_getPOIData: function(bounds){
		var params = {
			level: this.map.getZoom(),
			minx: bounds.left,
			miny: bounds.bottom,
			maxx: bounds.right,
			maxy: bounds.top
		};
		
		if(this.getPOIData){
			this.getPOIData(params);
		}else{
			var scope = this;
			$.ajax({
				type: "post",
				dataType: "JSON",
				url: this.url,
				data: params,
				success: function(json){
					scope.executeData(json);
				}
			});
		}
	},
	
	executeData: function(json){
		var data = json.data;
		var total = json.total;
		if(total != 0 && data){
			this.gridPOIs = {};
			var res = this.map.getResolution();
			var llo = res * this.iconhalfwidth;
			for(var i in data){
				var poi = data[i];
				poi.x = poi.POIX;
				poi.y = poi.POIY;
				poi.title = poi.POITITLE;
				var bounds = new OpenLayers.Bounds(poi.x - llo,poi.y-llo,poi.x + llo,poi.y+llo);
				poi.bounds = bounds;
				
				var coord = this.caculatePointCoord(poi.x, poi.y);
				var key = coord[0]+"_"+coord[1];
				this.gridPOIs[key] ? this.gridPOIs[key].push(poi) : this.gridPOIs[key] = [poi];
			}
		}
	},
	
	caculatePointCoord: function(x, y){
		var res = this.map.getResolution();
		var extent = this.map.getMaxExtent();

		var x = Math.floor((x - extent.left)/res/256);
		var y = Math.floor((y - extent.bottom)/res/256);
		
		return [x,y];
	},
	
	
	isMouseOnPOI: function(evt){
		if(this.gridPOIs){
			var lonlat = this.map.getLonLatFromPixel(evt.xy);
			var coord = this.caculatePointCoord(lonlat.lon, lonlat.lat);
			var key = coord[0]+"_"+coord[1];
			if(this.gridPOIs[key]){
				var pois = this.gridPOIs[key];
				for(var i in pois){
					var poi = pois[i];
					if(poi.bounds.contains(lonlat.lon, lonlat.lat)){
						return poi;
					}
				}
			}
		}else{
			return null;
		}
	},
	
	onMouseMove: function(evt){
		//清除掉标注信息
		var owner = this.owner;
		owner.labelLayer.clear();
		var poidata = this.marker.data;
		
		//添加标注
		var title = poidata.title || poidata.TITLE;
		var label = new OpenLayers.Label(this.marker.lonlat, title, "poiLabel");
		label.offset = this.icon.labelOffset;
		owner.labelLayer.add(label);
	},
	
	onMouseOut: function(){
		//清除掉标注信息
		var owner = this.owner;
		owner.labelLayer.clear();
		
		this.marker.icon.display(false);
	},
	
	removeMap: function(objmap) {
		OpenLayers.Layer.PoiLayer.prototype.removeMap.apply(this, arguments);
		objmap.events.unregister("mousemove", this, this.updateMarker);
	},
	
	CLASS_NAME: "OpenLayers.Layer.TiledPoiLayer"
});